// AttachMatrixFunction.cpp: implementation of the AttachMatrixFunction class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "phsshare.h"
#include "AttachMatrixFunction.h"
#include "JointFunction.h"
#include "Joint.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CREATE(AttachMatrixFunction,JointOwnFunction, FunctionObject::eJONTATTACHMATRIX);

AttachMatrixFunction::AttachMatrixFunction()
{

}

AttachMatrixFunction::~AttachMatrixFunction()
{
	
}

void AttachMatrixFunction::BasePointFormer(const CString& strFormer)
{
	INT nTemp = -1;
	m_basePtFormer.SetBy(nTemp, strFormer, 0, strFormer.GetLength() - 1);
}

void AttachMatrixFunction::vXFormer(const CString& strFormer)
{
	INT nTemp = -1;
	m_vXFormer.SetBy(nTemp, strFormer, 0, strFormer.GetLength() - 1);
}

void AttachMatrixFunction::vZFormer(const CString& strFormer)
{
	INT nTemp = -1;
	m_vZFormer.SetBy(nTemp, strFormer, 0, strFormer.GetLength() - 1);
}

FunctionObject* AttachMatrixFunction::InstanceFunction(Joint* pJoint)
{
	AttachMatrixFunction* pFun = new AttachMatrixFunction;

	pFun->m_basePt.Set(CaculateString((CString)(m_basePtFormer.x), pJoint->GetPropertyStack()),
					   CaculateString((CString)(m_basePtFormer.y), pJoint->GetPropertyStack()),
					   CaculateString((CString)(m_basePtFormer.z), pJoint->GetPropertyStack()));

	pFun->m_vX.Set(CaculateString((CString)(m_vXFormer.x), pJoint->GetPropertyStack()),
				   CaculateString((CString)(m_vXFormer.y), pJoint->GetPropertyStack()),
				   CaculateString((CString)(m_vXFormer.z), pJoint->GetPropertyStack()));

	pFun->m_vZ.Set(CaculateString((CString)(m_vZFormer.x), pJoint->GetPropertyStack()),
				   CaculateString((CString)(m_vZFormer.y), pJoint->GetPropertyStack()),
				   CaculateString((CString)(m_vZFormer.z), pJoint->GetPropertyStack()));

	pFun->m_strPartID = m_strPartID;
	pFun->SetOwner(pJoint);
	pJoint->AttachFunction(pFun);

	return pFun;
}

BOOL AttachMatrixFunction::Execute()
{
	ASSERT(FALSE);
	return TRUE;
}

AcGePoint3d AttachMatrixFunction::BasePoint()
{
	AcGePoint3d pt;
	pt.set(atof(CaculateString((CString)(m_basePt.x))),
		   atof(CaculateString((CString)(m_basePt.y))),
		   atof(CaculateString((CString)(m_basePt.z))));

	return pt;
}

AcGeMatrix3d AttachMatrixFunction::Matrix()
{
	AcGeMatrix3d matrix;
	
	AcGeVector3d vX;
	AcGeVector3d vY;
	AcGeVector3d vZ;

	vX.set(atof(CaculateString((CString)(m_vX.x))),
		   atof(CaculateString((CString)(m_vX.y))),
		   atof(CaculateString((CString)(m_vX.z))));

	
	vZ.set(atof(CaculateString((CString)(m_vZ.x))),
		   atof(CaculateString((CString)(m_vZ.y))),
		   atof(CaculateString((CString)(m_vZ.z))));

	matrix.setCoordSystem(BasePoint(),vX, vZ.crossProduct(vX), vZ);

	return matrix;
}